<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/wesite/view/common/base.jsp"%>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <script>var ctx="${ctx}";</script>
    <script type="text/javascript" src="${webctx}/script/jquery-1.8.3.min.js"></script>
    <script type="text/javascript" src="${webctx}/script/aui-dialog.js" ></script>
    <script type="text/javascript" src="${webctx}/script/aui-scroll.js" ></script>
    <script type="text/javascript" src="${webctx}/script/aui-toast.js"></script>
    <script type="text/javascript" src="${webctx}/script/productinfo.js"></script>
    <script type="text/javascript" src="${webctx}/script/common.js"></script>
    <script type="text/javascript" src="${webctx}/script/location.js"></script>
    <title>常购清单</title>
    <link rel="stylesheet" type="text/css" href="${webctx}/css/aui-slide.css"/>
    <%@ include file="/wesite/view/common/common.jsp"%>
    <style>
        .buy-btn2 a{
            position: absolute;
            top:15%;
            left:2.2rem;
            width:5.333333rem;
            height: 5.333333rem;
            text-align: center;
            line-height:5.333333rem;
            color: #e0e0e0;
            opacity: .5;
            background: #000;
            z-index: 12;
        }
        .buy-btn3 a{
            position: absolute;
            top:16%;
            left:2.2rem;
            width:5.333333rem;
            height: 5.333333rem;
            text-align: center;
            line-height:5.333333rem;
            color: #e0e0e0;
            opacity: .5;
            background: #000;
            z-index: 12;
        }
        .my-collect-bot span{
            display: block;
            width: 15.466667rem;
            height: 2.346667rem;
            background: #0daeff;
            border-radius: 5px;
            -webkit-border-radius: 5px;
            text-align: center;
            color: #fff;
            font-size: 0.8rem;
            line-height: 2.346667rem;
            margin:0 auto;
            margin-top: .43rem;
        }
        .btn-size{
            position: fixed;
            bottom:0;
            left: 0;
            width: 100%;
            /*padding: 0.875rem 2.3125rem;*/
            height: 3.375rem;
            background: #FFF;
            z-index: 13;
            border-top: 1px solid #e5e5e5;
        }
        .title{
            display: block;
            text-align: center;
            font-size: 15px;
            padding: 10px;
        }
        .img{
            display: block;
            margin: 0 auto;
        }
        .Explain{
            display: block;
            text-align: center;
            font-size: 15px;"
        }
        .Manufactor{
            overflow: hidden;
            text-overflow: ellipsis;
            white-space:nowrap;
        }
    </style>
</head>

<body>
<c:if test="${list.size()>0 }">
<div class="shopping-cart">
    <div class="hd">
        <div class="fl">
            <span>共：<i id="count" name="count"></i>件商品</span>
            <span>已选<i id="sun"></i>件</span>
        </div>
        <div class="fr allbox">
                <span>
                    <input type="checkbox"  name="cartcheckbox" id="selectAll" value="${product.id}" />
                    <i>全选</i>
                </span>
            <span onclick="deloften()">删除</span>
        </div>
    </div>
    <div class="cl"></div>
    <ul class="list">
        <c:forEach items="${list }" var="product" varStatus="status">
            <input type="hidden" name="errCode" value="${product.can}">
            <li>
                        <c:if test="${product.status != 2}">
                            <p class="buy-btn2" ><a>已下架</a></p>
                        </c:if>
                        <c:if test="${product.status == 2  && product.amount <= 0}">
                            <p class="buy-btn3"><a>缺货</a></p>
                        </c:if>
                        <c:if test="${product.status == 2 && product.amount > 0 && !product.can}">
                            <p class="buy-btn3"><a></a></p>
                        </c:if>
                <div class="selectbox"><input name="normalProduct" class="productbox" type="checkbox" value="${product.id}"></div>
                <div class="img">
                    <a href="${ctx }/we/product/${product.id}" ><img src="<%=getRandomFileserver()%>${product.photo}" width="90" height="90" alt="" onerror="this.src='${webctx }/images/noimg.jpg'"></a>
                </div>
                <div class="con" name="id" id="${product.id }">
                    <h3 class="aui-ellipsis-1">${product.productname } ${product.name }</h3>
                    <div class="data">
                        <p class="Manufactor">
                            <c:out value="${product.B_chanjia}" />
                        </p>
                        <p>
                            ${product.B_guige }
                        </p>
                        <span class="price">
                            <i>
                                <c:choose>
                                    <c:when test="${!islogin}">登录可见</c:when>
                                    <c:when test="${!isuserauth}">审核通过可见</c:when>
                                    <c:when test="${product.status == 2 && product.amount > 0 && !product.can}">该商品不在销售区域内</c:when>
                                    <c:otherwise>￥${product.price }</c:otherwise>
                                </c:choose>
                            </i>
                        </span>
                        <div class="count-box">
                            <i class="sy_minus" onclick="lessenamount(${product.id },this)">-</i>
                            <input name="buynumInp" type="number" class="num focus-auto-select" <c:if test='${product.amount>0}'>value="1"</c:if><c:if test='${product.amount<=0}'>value="0"</c:if> onchange="inputAmount(this)" maxnum="${product.amount }"/>
                            <i class="sy_plus" onclick="addamount(${product.id },this)">+</i>
                        </div>
                    </div>
                </div>
            </li>
        </c:forEach>
    </ul>
    <div class="h98"></div>
    <div class="h98"></div>
    <div class="my-collect-bot btn-size">
        <span onclick="addtocart()">加入购物车</span>
    </div>
    <input type="hidden" name="PageCount" />
</div>
</c:if>
<c:if test="${list.size()<=0 }">
    <div class="empty-page">
        <span class="txt-kong-img"><img src="${webctx}/images/Thome.png"></span>
        <p>您尚未添加商品 <a href="javascript:location.href='/we/index'">点我去逛逛~</a></p>
    </div>
</c:if>

</body>
<script type="text/javascript">
    apiready = function(){
        api.parseTapmode();
    }
    /*商品总数*/
    $(function () {
        $("#count").text($("input[type=checkbox]").length-1);
    })
    /*勾选商品数量*/
    $(function () {
        var getSun = function () {
            $("#sun").text($(".selectbox input:checkbox:checked").length);
        }
        getSun();

    })
    /*全选*/
    $("#selectAll").click(function () {
        var isChecked = $(this).prop("checked");
        $(".productbox").prop("checked", isChecked);
        $("#sun").text($(".selectbox input:checkbox:checked").length);
    })
    /*单选*/
    $(function () {
        $(".productbox").click(function() {
            if($("#selectAll").prop('checked')) {
                $(".productbox").each(function(i, ck) {
                    if (!$(ck).prop('checked')) {
                        $("#selectAll").prop("checked",false);
                    }
                });

            }else{
                var i = 0;
                $(".productbox").each(function() {
                    if ($(this).prop('checked')) {
                        i++;
                    }

                });
                if ($(".productbox").length==i) {
                    $("#selectAll").prop("checked",true);
                }
            }
            $("#sun").text($(".selectbox input:checkbox:checked").length);
        })
    })
    /*加商品数量*/
    function addamount(pid,obj){
        var txt=$("[name='buynumInp']",$(obj).parent());
        var n=parseInt(txt.val());
        n++;
        txt.val(n);
        inputAmount(txt);
    }
    /*减商品数量*/
    function lessenamount(pid,obj){
        var txt=$("[name='buynumInp']",$(obj).parent());
        var n=parseInt(txt.val());
        n--;
        txt.val(n);
        inputAmount(txt);
    }
    /*全选加入购物车*/
    function addtocart(){
        var  toast = new auiToast({});//土司弹出
        var dialog = new auiDialog({});//对话框弹出
        var chk_cart =[];
        $('input[name="normalProduct"]:checked').each(function(){
            var amount=$(this).parent().parent().find("input[name='buynumInp']").val();
            chk_cart.push($(this).val()+"_"+amount);
        });
        if (chk_cart.length > 0){
            $w.httpRequest({
                url:"${ctx}/we/home/addToCart",
                data:{"idsAmounts":chk_cart},
                ok:function(ret){
                    dialog.alert({
                        title:"提示",
                        msg:'加入购物车成功',
                        buttons:['确定']
                    })
                },
                fail:function (ret) {
                    if (ret.MsgCode) {
                        dialog.alert({
                            title:"提示",
                            msg:'加入购物车失败',
                            buttons:['确定']
                        })
                    } else {
                        dialog.alert({
                            title:"提示",
                            msg:'加入购物车失败',
                            buttons:['确定']
                        })
                    }
                }
            })
        }else {
            dialog.alert({
                title:"提示",
                msg:'您还没有选择商品',
                buttons:['确定']
            })
        }
    }
    /*全选,单选删除商品*/
    function deloften(){
        var dialog = new auiDialog({});//对话框弹出
        var chk_value =[];
        dialog.alert({
            title:"确认提示",
            msg:'确认要删除商品吗？',
            buttons:['取消','确定'],
        },function (ret) {
            if (ret){
                if (ret.buttonIndex == 1) {

                }
                if (ret.buttonIndex == 2) {
                    $('input[name="normalProduct"]:checked').each(function(){

                        chk_value.push($(this).val());
                    });
                    var del_ids={};
                    for (var i = 0; i < chk_value.length; i++) {
                        del_ids["ids[" + i + "]"] = chk_value[i];
                    }
                    //alert(chk_value.length==0 ?'你还没有选择任何商品！':chk_value);
                    if (chk_value.length > 0){
                        $w.httpRequest({
                            url:"${ctx}/we/home/delOften",
                            data:{"ids":chk_value},
                            ok:function(ret){
                                new auiToast().success({
                                    title: "删除成功",
                                    duration: 800
                                });
                                window.location.replace("${ctx}/we/home/oftenList");
                            }
                        })
                    }else {
                        dialog.alert({
                            title:"提示",
                            msg:'请选择要删除的商品',
                            buttons:['确定']
                        })
                    }
                }
            }
        })
    }

    function query(){
        gopage(1);
    }
    function gopage(n){
        var url = ${ctx}"/order/always-buy?pno="+n+"&like="+encodeURIComponent($("[name='like']").val());
        window.location.href=url;

    }
    function topageclick(){
        if($("[name='topagenum']").val()!="")
            gopage($("[name='topagenum']").val());
    }
    var submiting=false;
    function buyseled(){
        var seldatas=[];
        $(".productbox").each(function(){
            var box=$("[name='cartcheckbox']",$(this)).get(0);
            var baseobj=$("[pid='"+box.value+"']");
            if(box.checked){
                seldatas.push({"priceid":box.value,"num":$("[name='buynumInp']",baseobj).val()});
            }
        })
        if($w.checkLogin(true)){
            if(submiting)return;
            submiting=true;
            $w.httpRequest({
                url:apiroot+"/order/generateorder2",
                data:{"data":JSON.stringify(seldatas)},
                ok:function(ret){
                    var ordercode=ret.Data;
                    window.location.href=${ctx}"/order/submit?ordercode="+ordercode;
                },fail:function(ret){
                    if(ret.MsgCode=='1001')window.location.href=${ctx}"/user/certification";
                    else if(ret.MsgCode=='101'){
                        var h="以下商品库存不足：";
                        var list=window.eval(ret.MsgDesc);
                        for(var i=0;i<list.length;i++){
                            h+="\r\n"+(i+1)+"、"+list[i].productname+(list[i].attrname || '');
                        }
                        alert(h);
                    }else if(ret.MsgCode=='102'){
                        var h="以下商品已下架：";
                        var list=window.eval(ret.MsgDesc);
                        for(var i=0;i<list.length;i++){
                            h+="\r\n"+(i+1)+"、"+list[i].productname+(list[i].attrname || '');
                        }
                        alert(h);
                    }else if(ret.MsgCode=='103'){
                        var h="以下商品已不在您的可购范围：";
                        var list=window.eval(ret.MsgDesc);
                        for(var i=0;i<list.length;i++){
                            h+="\r\n"+(i+1)+"、"+list[i].productname+(list[i].attrname || '');
                        }
                        alert(h);
                    }else alert(ret.MsgDesc);
                    return false;
                },all:function(){submiting=false;}
            })
        }
    }
    function buyall(){
        if($w.checkLogin(true)){
            if(submiting)return;
            submiting=true;
            $w.httpRequest({
                url:apiroot+"/order/generateorder2",
                data:{"type":"alloften","data":$("[name='like']").val()},
                ok:function(ret){
                    var ordercode=ret.Data;
                    window.location.href=${ctx}"/order/submit?ordercode="+ordercode;
                },fail:function(ret){
                    if(ret.MsgCode=='1001')window.location.href=${ctx}"/user/certification";
                    else if(ret.MsgCode=='101'){
                        var h="以下商品库存不足：";
                        var list=window.eval(ret.MsgDesc);
                        for(var i=0;i<list.length;i++){
                            h+="\r\n"+(i+1)+"、"+list[i].productname+(list[i].attrname || '');
                        }
                        alert(h);
                    }else if(ret.MsgCode=='102'){
                        var h="以下商品已下架：";
                        var list=window.eval(ret.MsgDesc);
                        for(var i=0;i<list.length;i++){
                            h+="\r\n"+(i+1)+"、"+list[i].productname+(list[i].attrname || '');
                        }
                        alert(h);
                    }else if(ret.MsgCode=='103'){
                        var h="以下商品已不在您的可购范围：";
                        var list=window.eval(ret.MsgDesc);
                        for(var i=0;i<list.length;i++){
                            h+="\r\n"+(i+1)+"、"+list[i].productname+(list[i].attrname || '');
                        }
                        alert(h);
                    }else alert(ret.MsgDesc);
                    return false;
                },all:function(){submiting=false;}
            })
        }
    }
    function selall(ischeck){
        $(".productbox").each(function(){
            checkbox($(this),ischeck);
        })
    }
    function checkbox(obj,ischeck){
        if(ischeck){
            if(obj.get(0).className.indexOf("checked")==-1){
                obj.addClass("checked");
            }
        }else obj.removeClass("checked");
        $("[name='cartcheckbox']",obj).get(0).checked=ischeck;

    }

    function loadMaylike(size,fun,pno){
        var priceid=getBrowsepriceid();
        var url=apiroot+'/product/maylike?priceid='+priceid+"&pagesize="+size;
        if(pno)url+="&pageno="+pno;
        $w.httpRequest({
            url:url,
            data:{

            },
            ok:function(ret){
                fun(ret.Data);
            }
        })
    }

</script>

</html>